package jp.gr.java_conf.yuzu_project.parts;

import jp.gr.java_conf.yuzu_project.R;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.TextView;

/**
 * 文字列表示用クラス.
 * @author ゆずプロジェクト
 *
 */
public class StringDispSample {
	
    private String str = null;
    private TextView tx;
    private String put_word = "";
    private String put_text = "";
    private boolean clickFlg = false;
    private int messageNum = 0;
    private int i = 0;
     
    private int wordSpeed = 200;
    private static int TIMEOUT_MESSAGE = 1;
    
    private Handler hundler = new Handler() {
    	public void dispatchMessage(Message msg){
            char data[] = str.toCharArray();
            int arr_num = data.length;
            
            if(i < arr_num + 1){
            	if(msg.what == TIMEOUT_MESSAGE){
            		if(i < arr_num){
                		put_word = String.valueOf(data[i]);            			
            		}else{
            			put_word = " >>";
            		}
            		put_text = put_text + put_word ;
            		
            		tx.setText(put_text);
            		if(i >= arr_num){
            		    tx.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_launcher, 0);
            		}
            		i++;
            		hundler.sendEmptyMessageDelayed(TIMEOUT_MESSAGE, wordSpeed);
            	}else{
            		super.dispatchMessage(msg);
            	}
            } else {
            	i = 0;
            	put_text = "";
            	clickFlg = false;
            }
            
    	}	
    	
    };
	
	public StringDispSample(final String[] wordBox, final int message, final TextView tx, final FrameLayout layout) {
		this.tx = tx;
		tx.setTextColor(Color.BLACK);
		messageNum = message;
		
		layout.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				if (!clickFlg) {
					clickFlg = true;
					tx.setText(null);
					str = wordBox[messageNum];
					hundler.sendEmptyMessage(TIMEOUT_MESSAGE);
					messageNum++;
				}
			}
		});
	}
    
	public void changeSpeed() {
		wordSpeed = 300;
	}
 
}